// we7/pages/index/editAddress.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // editAddress:"",
    region: ['', '', ''],
    xuan: false,
    name: "",
    sex: "",
    address_id:"",
    // province: "",
    city: "",
    district:"",
    address: "",
    mobile:"",
    shadow:false,
    

  },
  // btn按钮
  SetShadow(e) {
    this.setData({
      shadow: e.detail.value
    })
  },
  // 城市联动
  bindRegionChange: function (e) {
    
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region:e.detail.value,
      xuan: false,
      province: e.detail.value
    })
  },
  nan: function () {
    this.setData({
      nanLogo: "icon-roundcheckfill",
      nvLogo: "icon-round",
      sex: 1
    })
  },
  address:function(e){
    this.setData({
      address:e.detail.value
    })
  },
  phone: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  nv: function () {
    this.setData({
      nanLogo: "icon-round",
      nvLogo: "icon-roundcheckfill",
      sex: 2
    })
  },
  
  nameHuo: function (e) {

    this.setData({
      name: e.detail.value
    })
  },
  mobileHuo: function (e) {

    this.setData({
      mobile: e.detail.value
    })
  },
  detailHuo: function (e) {

    this.setData({
      detail: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  // 点击提交收货地址
  tijiao:function(){
    var data=this.data;
    
    app.util.request({
      url: 'admin/WxGoodsApi/submitEditAddress',
      data: {
        uid: wx.getStorageSync('openid'),
        address_id:data.address_id,
        name:data.name,
        mobile:data.mobile,
        sex:data.sex,
        province:data.region[0],
        city:data.region[1],
        district:data.region[2],
        address:data.address,
        status: data.shadow,
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if(res.data.code==0){
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          setTimeout(function () {
            wx.navigateTo({
              url: './XSMSChooseAddressDetail'
            })
          }, 2000)
          
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
         
      },
      fail: function () {
        wx.showToast({
          title: '服务器未响应',
          icon: 'none',
          duration: 2000
        })
      }
    })

  },
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '编辑收货地址',
    })
    var that = this;
    var address_id = options.address_id
    app.util.request({
      url: 'admin/WxGoodsApi/editAddress',
      data: {
        address_id: address_id,
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          var data = res.data.data;
          // console.log(data)
          that.setData({
            address_id:address_id,
            name:data.name,
            sex:data.sex,
            region: [data.province,data.city,data.district],
            address: data.address,
            mobile: data.mobile
          })
        }
        if (that.data.sex === 1) {
          that.setData({
            nanLogo: "icon-roundcheckfill",
            nvLogo: "icon-round",
          })
        }
        if (that.data.sex === 2) {
          that.setData({
            nanLogo: "icon-round",
            nvLogo: "icon-roundcheckfill",
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器未响应',
          icon: 'none',
          duration: 2000
        })
      }
    })
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})